package org.tea.inventory.admin.web.admin;

import org.tea.inventory.admin.annotation.LoginAdmin;
import org.tea.inventory.base.util.ResponseUtil;
import org.tea.inventory.db.domain.QuestionSort;
import org.tea.inventory.db.service.QuestionSortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/admin/questionSort")
public class QuestionSortController {

    @Autowired
    private QuestionSortService questionSortService;

    @GetMapping("list")
    public Object list(@LoginAdmin Integer adminId,
                       @RequestParam(value = "page", defaultValue = "1")Integer page,
                       @RequestParam(value = "limit", defaultValue = "10")Integer limit,
                       QuestionSort questionSort) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }

        Map<String,Object> data = questionSortService.selectByExample(questionSort,page,limit);

        return ResponseUtil.ok(data);
    }

    /**
     * 获取指定id的问题种类
     */
    @GetMapping("read")
    public Object read(@LoginAdmin Integer adminId, Integer id) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }

        if(id == null) {
            return  ResponseUtil.badArgumentValue();
        }

        QuestionSort questionSort = questionSortService.selectByPrimaryKey(id);

        return ResponseUtil.ok(questionSort);
    }

    /**
     * 创建问题种类信息
     */
    @PostMapping("create")
    public Object create(@LoginAdmin Integer adminId, @RequestBody QuestionSort questionSort) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }
        questionSort.setQuestionNum(0);
        questionSortService.insert(questionSort);
        return ResponseUtil.ok(questionSort);
    }

    /**
     * 更新问题种类信息
     */
    @PostMapping("update")
    public Object update(@LoginAdmin Integer adminId, @RequestBody QuestionSort questionSort) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }
        questionSortService.updateByPrimaryKeySelective(questionSort);

        return ResponseUtil.ok(questionSort);
    }

    /**
     * 删除问题种类信息
     */
    @PostMapping("delete")
    public Object delete(@LoginAdmin Integer adminId, @RequestBody QuestionSort questionSort) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }

        if(questionSort == null || questionSort.getId() == null) {
            return ResponseUtil.badArgumentValue();
        }
        questionSortService.deleteByPrimaryKey(questionSort.getId());
        return ResponseUtil.ok();
    }
}
